31 #define _CRT_SECURE_NO_WARNINGS 33 #define __PROTOCOL_INCLUDE_ENDPOINT_ONLY_API__ 34 #include "Protocol_internal.h" 35 #undef __PROTOCOL_INCLUDE_ENDPOINT_ONLY_API__ 43 #undef _CRT_SECURE_NO_WARNINGS 61 #define CNST_STARTBYTE_DATA 0x5A 63 #define CNST_STARTBYTE_STATUS 0x5B 65 #define CNST_END_OF_PAYLOAD 0xE0DB 70 #define CNST_MSG_QUERY_ENDPOINT_INFO 0x00 74 #define CNST_MSG_ENDPOINT_INFO 0x00 78 #define CNST_MSG_QUERY_FW_INFO 0x01 82 #define CNST_MSG_FW_INFO 0x01 86 #define CNST_MSG_FIRMWARE_RESET 0x02 90 #define CNST_PROTOCOL_RECEIVED_PAYLOAD_MSG 0x01000000 109 const Endpoint_Definition_t* endpoint_definiton;
136 uint8_t num_endpoints;
138 Endpoint_t* endpoints;
143 size_t(*get_data)(
void* port_handle,
void* data,
size_t num_requested_bytes);
146 size_t (*send_data)(
void* port_handle,
const void* data,
size_t num_bytes);
149 void (*close)(
void* port_handle);
169 uint16_t payload_size;
213 static Instance_t* handles = NULL;
215 static size_t num_allocated_handles = 0;
217 static size_t num_open_handles = 0;
224 #define __PROTOCOL_INCLUDES_ENDPOINT_LIST__ 225 #include "Protocol_KnownEndpoints.h" 226 #undef __PROTOCOL_INCLUDES_ENDPOINT_LIST__ 235 #define NUM_KNOWN_ENDPOINTS \ 236 (sizeof(known_endpoints) / sizeof(const Endpoint_Definition_t*)) 259 static void recover_from_receive_error(
const Instance_t* protocol);
280 static void send_message(
const Instance_t* protocol, uint8_t endpoint,
281 const uint8_t* payload, uint16_t payload_size);
312 static int32_t get_message(
const Instance_t* protocol,
313 Message_Info_t* message_info);
325 static void recover_from_receive_error(
const Instance_t* protocol)
328 uint8_t dummy_data[1024];
329 size_t received_bytes =
sizeof(dummy_data);
331 while (received_bytes ==
sizeof(dummy_data))
333 received_bytes = protocol->get_data(protocol->port_handle,
334 dummy_data,
sizeof(dummy_data));
340 static void send_message(
const Instance_t* protocol, uint8_t endpoint,
341 const uint8_t* payload, uint16_t payload_size)
344 uint8_t message_header[4];
345 uint8_t message_tail[2];
348 message_header[1] = endpoint;
349 message_header[2] = payload_size & 0xFF;
350 message_header[3] = payload_size >> 8;
356 size_t message_size = (size_t)4 + (
size_t)payload_size + (size_t)2;
357 uint8_t* message = malloc(message_size *
sizeof(uint8_t));
362 memcpy(message, message_header,
sizeof(message_header));
363 memcpy(message+
sizeof(message_header), payload, payload_size);
364 memcpy(message +
sizeof(message_header) + payload_size, message_tail,
sizeof(message_tail));
367 protocol->send_data(protocol->port_handle, message, message_size);
372 static int32_t get_message(
const Instance_t* protocol,
373 Message_Info_t* message_info)
375 uint8_t message_header[4];
376 size_t num_received_bytes;
380 message_info->endpoint = 0;
381 message_info->payload = NULL;
382 message_info->payload_size = 0;
386 num_received_bytes = protocol->get_data(protocol->port_handle,
388 sizeof(message_header));
396 if ((num_received_bytes <
sizeof(message_header)))
398 num_received_bytes +=
399 protocol->get_data(protocol->port_handle,
400 message_header + num_received_bytes,
401 sizeof(message_header) - num_received_bytes);
407 if (num_received_bytes == 0)
415 else if (num_received_bytes <
sizeof(message_header))
417 recover_from_receive_error(protocol);
426 uint16_t payload_size;
427 uint8_t message_tail[2];
430 payload_size = (uint16_t)message_header[2] |
431 ((uint16_t)message_header[3]) << 8;
432 payload = (uint8_t*)malloc(payload_size);
434 num_received_bytes = protocol->get_data(protocol->port_handle,
435 payload, payload_size);
438 if (num_received_bytes < payload_size)
443 recover_from_receive_error(protocol);
449 num_received_bytes = protocol->get_data(protocol->port_handle,
451 sizeof(message_tail));
453 if ((num_received_bytes !=
sizeof(message_tail)) ||
460 recover_from_receive_error(protocol);
465 message_info->endpoint = message_header[1];
466 message_info->payload = payload;
467 message_info->payload_size = payload_size;
473 uint8_t endpoint = message_header[1];
474 uint16_t status_code = (uint16_t)message_header[2] |
475 ((uint16_t)message_header[3]) << 8;
477 return ((
int)endpoint << 16) | status_code;
481 recover_from_receive_error(protocol);
496 static char* strclone(
const char* s)
498 size_t size = strlen(s) + 1;
499 char* copy = calloc(size,
sizeof(
char));
501 memcpy(copy, s, size);
507 Port_Info_t port_info = { 0 };
509 if (strlen(port_name) <= 6)
512 if (strncmp(port_name,
"udp://", 6) == 0)
515 port_info.is_udp = 1;
517 else if (strncmp(port_name,
"tcp://", 6) == 0)
519 port_info.is_tcp = 1;
527 char* server_port = strclone(port_name + 6);
531 p = strchr(server_port,
':');
535 if (strlen(server_port) <=
sizeof(port_info.ip4addr))
537 strcpy(port_info.ip4addr, server_port);
538 port_info.port = atoi(p + 1);
556 Instance_t protocol_instance = { 0 };
557 Message_Info_t message_info;
558 int32_t receive_status;
567 protocol_instance.port_handle = com_port_handle;
568 protocol_instance.get_data = (size_t(*)(
void*,
void*, size_t))
com_get_data;
569 protocol_instance.send_data = (size_t (*)(
void*,
const void*, size_t))
com_send_data;
570 protocol_instance.close = (void (*)(
void*))
com_close;
575 protocol_instance.num_endpoints = 0;
576 protocol_instance.endpoints = NULL;
582 send_message(&protocol_instance, 0, uQueryMessage,
sizeof(uQueryMessage));
587 message_info.payload = NULL;
588 receive_status = get_message(&protocol_instance, &message_info);
591 (message_info.endpoint != 0) ||
592 (message_info.payload_size < 2) ||
596 free(message_info.payload);
597 protocol_instance.close(protocol_instance.port_handle);
602 protocol_instance.num_endpoints = message_info.payload[1];
604 if ((message_info.payload_size !=
605 6 * protocol_instance.num_endpoints + 2) ||
606 (protocol_instance.num_endpoints == 0))
608 free(message_info.payload);
609 protocol_instance.close(protocol_instance.port_handle);
614 protocol_instance.endpoints =
615 (Endpoint_t*)malloc(protocol_instance.num_endpoints *
619 for (uint8_t i = 0; i < protocol_instance.num_endpoints; ++i)
622 Endpoint_t* endpoint = &protocol_instance.endpoints[i];
625 endpoint->type = protocol_read_payload_uint32(message_info.payload,
627 endpoint->version = protocol_read_payload_uint16(message_info.payload,
631 endpoint->endpoint_definiton = NULL;
635 if ((known_endpoints[j]->type == endpoint->type) &&
636 (known_endpoints[j]->min_version <= endpoint->version) &&
637 (known_endpoints[j]->max_version >= endpoint->version))
639 endpoint->endpoint_definiton = known_endpoints[j];
646 free(message_info.payload);
647 message_info.payload = NULL;
650 receive_status = get_message(&protocol_instance, &message_info);
652 if (receive_status != ((0 << 16) | 0x0000))
661 free(message_info.payload);
664 free(protocol_instance.endpoints);
665 protocol_instance.close(protocol_instance.port_handle);
673 if (num_open_handles == num_allocated_handles)
675 Instance_t* pNewHandles = (Instance_t*)malloc((num_open_handles + 1) *
678 if (num_allocated_handles > 0)
680 memcpy(pNewHandles, handles,
681 sizeof(Instance_t) * num_allocated_handles);
684 pNewHandles[num_allocated_handles++].num_endpoints = 0;
687 handles = pNewHandles;
693 while (handles[new_handle].num_endpoints != 0)
698 handles[new_handle] = protocol_instance;
707 if ((protocol_handle >= 0) &&
708 (protocol_handle < (int32_t)num_allocated_handles) &&
709 (handles[protocol_handle].num_endpoints != 0))
711 const Instance_t* instance = &handles[protocol_handle];
713 instance->close(instance->port_handle);
714 handles[protocol_handle].port_handle = NULL;
717 free (handles[protocol_handle].endpoints);
718 handles[protocol_handle].endpoints = NULL;
721 handles[protocol_handle].num_endpoints = 0;
727 if (num_open_handles == 0)
729 num_allocated_handles = 0;
739 if ((protocol_handle < 0) ||
740 (protocol_handle >= (int32_t)num_allocated_handles) ||
741 (handles[protocol_handle].num_endpoints == 0))
746 return handles[protocol_handle].num_endpoints;
752 Instance_t* protocol;
753 Message_Info_t message_info;
754 int32_t receive_status;
757 if ((protocol_handle < 0) ||
758 (protocol_handle >= (int32_t)num_allocated_handles) ||
759 (handles[protocol_handle].num_endpoints == 0))
764 protocol = &handles[protocol_handle];
770 send_message(protocol, 0, uQueryMessage,
sizeof(uQueryMessage));
774 message_info.payload = NULL;
775 receive_status = get_message(protocol, &message_info);
778 (message_info.endpoint != 0) ||
779 (message_info.payload_size < 7) ||
783 free(message_info.payload);
789 protocol_read_payload_uint16(message_info.payload, 1);
791 protocol_read_payload_uint16(message_info.payload, 3);
793 protocol_read_payload_uint16(message_info.payload, 5);
795 if (message_info.payload_size > 7)
797 uint16_t description_size = message_info.payload_size - 7;
798 information->
description = malloc(description_size);
799 memcpy(information->
description, message_info.payload + 7,
804 free(message_info.payload);
807 receive_status = get_message(protocol, &message_info);
809 if (receive_status != ((0 << 16) | 0x0000))
818 free(message_info.payload);
829 Instance_t* protocol;
830 Message_Info_t message_info;
831 int32_t receive_status;
834 if ((protocol_handle < 0) ||
835 (protocol_handle >= (int32_t)num_allocated_handles) ||
836 (handles[protocol_handle].num_endpoints == 0))
841 protocol = &handles[protocol_handle];
847 send_message(protocol, 0, uQueryMessage,
sizeof(uQueryMessage));
851 receive_status = get_message(protocol, &message_info);
853 if (receive_status != ((0 << 16) | 0x0000))
862 free(message_info.payload);
874 Endpoint_t* requested_endpoint;
877 if ((protocol_handle < 0) ||
878 (protocol_handle >= (int32_t)num_allocated_handles) ||
879 (handles[protocol_handle].num_endpoints == 0))
885 if ((endpoint == 0) ||
886 (endpoint > handles[protocol_handle].num_endpoints))
892 requested_endpoint = &handles[protocol_handle].endpoints[endpoint - 1];
894 endpoint_info->
type = requested_endpoint->type;
895 endpoint_info->
version = requested_endpoint->version;
899 if (requested_endpoint->endpoint_definiton)
902 requested_endpoint->endpoint_definiton->description;
910 const Endpoint_Definition_t*
920 if (sEndpointInfo.
type != endpoint_definiton->type)
925 if (sEndpointInfo.
version < endpoint_definiton->min_version)
930 if (sEndpointInfo.
version > endpoint_definiton->max_version)
940 const Endpoint_Definition_t*
942 const uint8_t* payload,
943 uint16_t payload_size)
945 Instance_t* protocol;
946 Message_Info_t message_info;
952 if ((protocol_handle < 0) ||
953 (protocol_handle >= (int32_t)num_allocated_handles) ||
954 (handles[protocol_handle].num_endpoints == 0))
959 protocol = &handles[protocol_handle];
962 if ((endpoint == 0) || (endpoint > protocol->num_endpoints))
968 if (protocol->endpoints[endpoint-1].endpoint_definiton !=
976 send_message(protocol, endpoint, payload, payload_size);
979 while ((status_code = get_message(protocol, &message_info)) ==
983 if (message_info.endpoint > 0)
985 const Endpoint_Definition_t* endpoint_ptr =
986 protocol->endpoints[message_info.endpoint-1].endpoint_definiton;
990 endpoint_ptr->parse_payload(protocol_handle,
991 message_info.endpoint,
992 message_info.payload,
993 message_info.payload_size);
997 free(message_info.payload);
1005 int32_t status_code)
1014 return "The device received a message at endpoint 0 that could not be understood.";
1017 if (status_code >= 0)
1021 uint8_t endpoint = (uint8_t)(status_code >> 16);
1022 uint16_t error_code = status_code & 0xFFFF;
1030 return "A time out occured during message transmission from host to device.";
1033 return "The device received a message with a bad start sequence.";
1036 return "The device received a message for a non existing endpoint.";
1039 return "The device received a data message without payload.";
1042 return "The device's memory is not sufficient to process the receive data message.";
1045 return "The device received a message with a bad end sequence.";
1048 return "Unknown Error";
1053 Endpoint_t* requested_endpoint;
1056 if ((protocol_handle < 0) ||
1057 (protocol_handle >= (int32_t)num_allocated_handles) ||
1058 (handles[protocol_handle].num_endpoints == 0))
1060 return "Invalid Handle.";
1064 if (endpoint > handles[protocol_handle].num_endpoints)
1066 return "Invalid Endpoint.";
1069 requested_endpoint =
1070 &(handles[protocol_handle].endpoints[endpoint-1]);
1073 if (requested_endpoint->endpoint_definiton)
1075 return requested_endpoint->endpoint_definiton->
1076 get_status_descr(error_code);
1079 return "Unknown Error";
1087 switch (status_code)
1090 return "The protocol handle is not a valid.";
1093 return "The specified COM port could not be opened.";
1096 return "The device at the specified COM port is not compatible with the protocol.";
1099 return "The device didn't send any message.";
1102 return "A timeout occurred while receiving a message from the device.";
1105 return "The device sent a message with a bad start sequence.";
1108 return "The device sent a message with a bad end sequence.";
1111 return "The requested endpoint does not exist.";
1114 return "The requested endpoint is not of the type checked for.";
1117 return "The requested endpoint's version is too old to be supported.";
1120 return "The requested endpoint's version is too new to be supported.";
1123 return "Unknown Error";
1134 Instance_t * protocol;
1135 Message_Info_t message_info;
1136 int32_t status_code;
1141 if ((protocol_handle < 0) ||
1142 (protocol_handle >= (int32_t)num_allocated_handles) ||
1143 (handles[protocol_handle].num_endpoints == 0))
1148 protocol = &handles[protocol_handle];
1151 status_code = get_message(protocol, &message_info);
1157 if (message_info.endpoint > 0)
1159 const Endpoint_Definition_t * endpoint_ptr =
1160 protocol->endpoints[message_info.endpoint - 1].endpoint_definiton;
1164 endpoint_ptr->parse_payload(protocol_handle,
1165 message_info.endpoint,
1166 message_info.payload,
1167 message_info.payload_size);
1171 free(message_info.payload);
Port_Info_t parse_port_name(const char *port_name)
int32_t protocol_do_firmware_reset(int32_t protocol_handle)
This function triggers a firmware reset of the connected device.
#define PROTOCOL_ERROR_ENDPOINT_VERSION_TOO_OLD
#define PROTOCOL_ERROR_COULD_NOT_OPEN_COM_PORT
size_t com_get_data(com_t *com_port, void *data, size_t num_requested_bytes)
This function reads data from an open COM port.
#define CNST_MSG_ENDPOINT_INFO
void com_close(com_t *com_port)
This function closes the specified COM port.
#define PROTOCOL_STATUS_DEVICE_NO_PAYLOAD
const char * protocol_get_status_code_description(int32_t protocol_handle, int32_t status_code)
This function returns a human readable description of a status or error code.
#define PROTOCOL_STATUS_DEVICE_BAD_PAYLOAD_END
#define PROTOCOL_ERROR_RECEIVED_NO_MESSAGE
#define CNST_MSG_QUERY_ENDPOINT_INFO
#define PROTOCOL_ERROR_RECEIVED_BAD_MESSAGE_START
#define PROTOCOL_STATUS_DEVICE_BAD_MESSAGE_START
This file defines the API to access a serial communication port.
#define PROTOCOL_ERROR_DEVICE_NOT_COMPATIBLE
int32_t protocol_get_endpoint_info(int32_t protocol_handle, uint8_t endpoint, Endpoint_Info_t *endpoint_info)
This function returns information about the requested endpoint.
This file defines the basic API to access Infineon sensor devices.
#define CNST_END_OF_PAYLOAD
#define PROTOCOL_STATUS_OK
int32_t protocol_poll_receive_once(int32_t protocol_handle)
size_t com_send_data(com_t *com_port, const void *data, size_t num_bytes)
This function sends data through an open COM port.
int32_t protocol_send_and_receive(int32_t protocol_handle, uint8_t endpoint, const Endpoint_Definition_t *endpoint_definiton, const uint8_t *payload, uint16_t payload_size)
int32_t protocol_get_num_endpoints(int32_t protocol_handle)
This function returns the number of endpoints in the connected device.
int32_t protocol_connect(const char *port_name)
This function establishes a connection to a device through a COM port.
int32_t protocol_get_firmware_information(int32_t protocol_handle, Firmware_Information_t *information)
This function returns the information about the firmware of the connected device.
void protocol_disconnect(int32_t protocol_handle)
This function closes an open connection.
#define PROTOCOL_ERROR_ENDPOINT_WRONG_TYPE
This structure contains information about an endpoint.
#define PROTOCOL_STATUS_DEVICE_TIMEOUT
#define PROTOCOL_STATUS_DEVICE_BAD_COMMAND
#define CNST_STARTBYTE_DATA
#define PROTOCOL_ERROR_RECEIVED_BAD_MESSAGE_END
int32_t protocol_is_endpoint_compatible(int32_t protocol_handle, uint8_t endpoint, const Endpoint_Definition_t *endpoint_definiton)
com_t * com_open(const char *port_name)
This functions opens the specified COM port.
#define PROTOCOL_ERROR_RECEIVED_TIMEOUT
#define PROTOCOL_ERROR_ENDPOINT_VERSION_TOO_NEW
#define PROTOCOL_STATUS_DEVICE_BAD_ENDPOINT_ID
#define CNST_STARTBYTE_STATUS
#define CNST_MSG_QUERY_FW_INFO
#define PROTOCOL_STATUS_DEVICE_OUT_OF_MEMORY
#define CNST_MSG_FIRMWARE_RESET
#define PROTOCOL_ERROR_ENDPOINT_DOES_NOT_EXIST
#define PROTOCOL_ERROR_INVALID_HANDLE
#define CNST_PROTOCOL_RECEIVED_PAYLOAD_MSG
#define NUM_KNOWN_ENDPOINTS